﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreBDS
{
    public class MemberBll
    {
        public static int Insert(nlit_Member Info, string strConnect)
        {
            int id = 0;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    db.nlit_Members.InsertOnSubmit(Info);
                    db.SubmitChanges();
                    id = Info.MemberId;
                }
                catch { id = 0; }
                finally { db.Dispose(); }
            }
            return id;
        }

        public static bool Update(nlit_Member Info, string strConnect)
        {
            bool res = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    nlit_Member o = db.nlit_Members.Single(p => p.MemberId == Info.MemberId);
                    if (o != null)
                    {
                        o.Address = Info.Address;
                        o.Alias = Info.Alias;
                        o.Birthday = Info.Birthday;
                        o.Email = Info.Email;
                        o.FullName = Info.FullName;
                        o.Mobiphone = Info.Mobiphone;
                        o.NickGmail = Info.NickGmail;
                        o.NickSkype = Info.NickSkype;
                        o.NickYahoo = Info.NickYahoo;
                        //o.Pass = Info.Pass;
                        o.ProvinceId = Info.ProvinceId;
                        o.Telephone = Info.Telephone;
                        o.UserName = Info.UserName;
                        o.WardId = Info.WardId;
                        
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateIsDeleted(int memberId, bool isDeleted, string strConnect)
        {
            bool res = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    nlit_Member o = db.nlit_Members.Single(p => p.MemberId == memberId);
                    if (o != null)
                    {
                        o.IsDeleted = isDeleted;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateIsActived(int memberId, bool isActived, string strConnect)
        {
            bool res = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    nlit_Member o = db.nlit_Members.Single(p => p.MemberId == memberId);
                    if (o != null)
                    {
                        o.IsActived = isActived;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static List<nlit_Member> GetList(bool isDeleted, bool isActived, string strConnect)
        {
            List<nlit_Member> List = new List<nlit_Member>();

            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Members
                                  where p.IsDeleted == isDeleted && p.IsActived == isActived
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }

        public static List<nlit_Member> GetList( bool isDeleted ,string strConnect)
        {
            List<nlit_Member> List = new List<nlit_Member>();

            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Members
                                  where p.IsDeleted == isDeleted
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }

        public static List<nlit_Member> GetList( string strConnect)
        {
            List<nlit_Member> List = new List<nlit_Member>();

            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Members
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }

        public static nlit_Member GetById(int memberId, string strConnect)
        {
            nlit_Member res = new nlit_Member();

            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {

                    res = db.nlit_Members.Single(p => p.MemberId == memberId);

                }
                catch
                {

                }
                finally
                {
                    db.Dispose();
                }
            }
            return res;
        }

        public static nlit_Member GetByEmail(string email, string strConnect)
        {
            nlit_Member res = new nlit_Member();

            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {

                    res = db.nlit_Members.Single(p => p.Email == email);

                }
                catch
                {

                }
                finally
                {
                    db.Dispose();
                }
            }
            return res;
        }

        public static bool EmailExist(string email, string strConnect)
        {
            bool res = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    nlit_Member o = db.nlit_Members.Single(p => p.Email == email);
                    if (o != null)
                    {
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UserNameExist(string userName, string strConnect)
        {
            bool res = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    nlit_Member o = db.nlit_Members.Single(p => p.UserName == userName);
                    if (o != null)
                    {
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        /// <summary>
        /// kiem tra dang nhap
        /// </summary>
        /// <param name="userName"> co the la Email hoac UserName</param>
        /// <param name="pass"></param>
        /// <param name="strConnect"></param>
        /// <returns></returns>
        public static bool CheckLogin(string userName, string pass, string strConnect)
        {
            bool res = false;
            using (BDSLinqDataContext db = new BDSLinqDataContext(strConnect))
            {
                try
                {
                    //nlit_Member o = db.nlit_Members.Single(((p=>p.UserName==userName) || (p1=>p1.Email==userName))&&(p2=>p2.Pass==pass));
                    nlit_Member o = db.nlit_Members.Single(p => p.UserName == userName && p.Pass==pass);
                    if (o != null)
                    {
                        res = true;
                    }
                    else
                    {
                        nlit_Member o1 = db.nlit_Members.Single(p => p.Email == userName && p.Pass == pass);
                        if (o1 != null)
                        {
                            res = true;
                        }
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }
    }
}
